home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / docs / linux-do / programm / lpg-0.4 / lpg-0 / LPG / examples / ipc / shmtool.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-01  |  2.8 KB  |  126 lines

  1. /*****************************************************************************
  2.  Excerpt from "Linux Programmer's Guide - Chapter 6"
  3.  (C)opyright 1994-1995, Scott Burkett
  4.  ***************************************************************************** 
  5.  MODULE: shmtool.c
  6.  *****************************************************************************
  7.  A command line tool for tinkering with SysV style Shared Memory Segments
  8.  *****************************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <ctype.h>
  13. #include <sys/types.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16.  
  17. #define SEGSIZE    100
  18.  
  19. void writeshm(int shmid, char *segptr, char *text);
  20. void readshm(int shmid, char *segptr);
  21. void removeshm(int shmid);
  22. void changemode(int shmid, char *mode);
  23. void usage(void);
  24.  
  25.  
  26.  
  27. int main(int argc, char *argv[])
  28. {
  29.     key_t key;
  30.     int   shmid;
  31.     char  *segptr;
  32.  
  33.     if(argc == 1)
  34.         usage();
  35.  
  36.     /* Create unique key via call to ftok() */
  37.     key = ftok(".", 'S');
  38.  
  39.     /* Open the shared memory segment - create if necessary */
  40.     if((shmid = shmget(key, SEGSIZE, IPC_CREAT|IPC_EXCL|0666)) == -1) 
  41.     {
  42.         printf("Shared memory segment exists - opening as client\n");
  43.  
  44.         /* Segment probably already exists - try as a client */
  45.         if((shmid = shmget(key, SEGSIZE, 0)) == -1) 
  46.         {
  47.             perror("shmget");
  48.             exit(1);
  49.         }
  50.     }
  51.     else
  52.     {
  53.         printf("Creating new shared memory segment\n");
  54.     }
  55.  
  56.     /* Attach (map) the shared memory segment into the current process */
  57.     if((segptr = shmat(shmid, 0, 0)) == (char *)-1)
  58.     {
  59.         perror("shmat");
  60.         exit(1);
  61.     }
  62.     
  63.     switch(tolower(argv[1][0]))
  64.     {
  65.         case 'w': writeshm(shmid, segptr, argv[2]);
  66.               break;
  67.         case 'r': readshm(shmid, segptr);
  68.               break;
  69.         case 'd': removeshm(shmid);
  70.               break;
  71.         case 'm': changemode(shmid, argv[2]);
  72.               break;
  73.          default: usage();
  74.  
  75.     }
  76.  
  77.     return(0);
  78. }
  79.  
  80. void writeshm(int shmid, char *segptr, char *text)
  81. {
  82.     strcpy(segptr, text);
  83.     printf("Done...\n");
  84. }
  85.  
  86. void readshm(int shmid, char *segptr)
  87. {
  88.     printf("segptr: %s\n", segptr);
  89. }
  90.  
  91. void removeshm(int shmid)
  92. {
  93.     shmctl(shmid, IPC_RMID, 0);
  94.     printf("Shared memory segment marked for deletion\n");
  95. }
  96.  
  97. void changemode(int shmid, char *mode) 
  98. {
  99.     struct shmid_ds myshmds;
  100.  
  101.     /* Get current values for internal data structure */
  102.     shmctl(shmid, IPC_STAT, &myshmds);
  103.  
  104.     /* Display old permissions */
  105.     printf("Old permissions were: %o\n", myshmds.shm_perm.mode);
  106.  
  107.     /* Convert and load the mode */ 
  108.     sscanf(mode, "%ho", &myshmds.shm_perm.mode);
  109.  
  110.     /* Update the mode */
  111.     shmctl(shmid, IPC_SET, &myshmds);
  112.  
  113.     printf("New permissions are : %o\n", myshmds.shm_perm.mode);
  114. }
  115.  
  116. void usage(void)
  117. {
  118.     fprintf(stderr, "shmtool - A utility for tinkering with shared memory\n");
  119.     fprintf(stderr, "\nUSAGE:  shmtool (w)rite <text>\n");
  120.     fprintf(stderr, "                (r)ead\n");
  121.     fprintf(stderr, "                (d)elete\n");
  122.     fprintf(stderr, "                (m)ode change <octal mode>\n");
  123.     exit(1);
  124. }
  125.  
  126.